package com.wjc.train.business.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.wjc.train.business.entity.DailyTrainStation;
import com.wjc.train.business.entity.TrainStation;
import com.wjc.train.business.repository.DailyTrainStationRepository;
import com.wjc.train.business.repository.TrainStationRepository;
import com.wjc.train.business.req.DailyTrainStationQueryReq;
import com.wjc.train.business.req.DailyTrainStationSaveReq;
import com.wjc.train.business.resp.DailyTrainStationQueryResp;
import com.wjc.train.business.service.IDailyTrainStationService;
import com.wjc.train.common.enums.LogicEnum;
import com.wjc.train.common.untils.CopyUtil;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 每日车次车站表 服务实现类
 * </p>
 *
 * @author Guessed
 * @since 2023-08-09
 */
@Service
@Slf4j
public class DailyTrainStationServiceImpl  implements IDailyTrainStationService {

    @Resource
    private DailyTrainStationRepository dailytrainstationRepository;

    @Resource
    private TrainStationRepository trainstationRepository;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void save(DailyTrainStationSaveReq dailytrainstationSaveReq) {
        DailyTrainStation dailytrainstation = CopyUtil.copy(dailytrainstationSaveReq, DailyTrainStation.class);
        dailytrainstationRepository.saveOrUpdate(dailytrainstation);
    }

    @Override
    public IPage<DailyTrainStationQueryResp> queryList(IPage<DailyTrainStationQueryResp> page, DailyTrainStationQueryReq dailytrainstationQueryReq) {
        return dailytrainstationRepository.queryList(page,dailytrainstationQueryReq);
    }

    @Override
    public void delete(Long id) {
        dailytrainstationRepository.getBaseMapper().deleteById(id);
    }

    @Override
    public void genDaily(Date date, String trainCode) {
        log.info("开始生成日期:{},车次:{}的车站信息", DateUtil.formatDate(date),trainCode);
        //查询当日车站信息并删除
        List<DailyTrainStation> dailyStationList = dailytrainstationRepository.lambdaQuery()
                .eq(DailyTrainStation::getDate, date)
                .eq(DailyTrainStation::getIsDeleted, LogicEnum.NOT_DELETED.getCode())
                .eq(DailyTrainStation::getTrainCode, trainCode).list();
        if(CollectionUtil.isNotEmpty(dailyStationList)){
            List<Long> collect = dailyStationList.stream().map(DailyTrainStation::getId).collect(Collectors.toList());
            dailytrainstationRepository.getBaseMapper().deleteBatchIds(collect);
        }
        //查询该车次的所有车站信息
        List<TrainStation> trainStationList = trainstationRepository.lambdaQuery()
                .eq(TrainStation::getTrainCode, trainCode)
                .eq(TrainStation::getIsDeleted, LogicEnum.NOT_DELETED.getCode()).list();
        if(CollectionUtil.isEmpty(trainStationList)){
            log.error("该车次不存在车站信息，车次编号为:{}",trainCode);
            return;
        }
        //生成每日车站信息
        List<DailyTrainStation> dailyTrainStations =
                CopyUtil.copyList(trainStationList, DailyTrainStation.class);
        dailyTrainStations.forEach(x->{
            x.setId(null);
            x.setDate(date);
        });
        dailytrainstationRepository.saveBatch(dailyTrainStations);
        log.info("结束生成日期:{},车次:{}的车站信息", DateUtil.formatDate(date),trainCode);

    }

    @Override
    public long countByTrainCode(Date trainDate,String trainCode) {
        return dailytrainstationRepository.lambdaQuery()
                            .eq(DailyTrainStation::getTrainCode, trainCode)
                            .eq(DailyTrainStation::getDate, trainDate)
                            .eq(DailyTrainStation::getIsDeleted, LogicEnum.NOT_DELETED.getCode())
                            .count();
    }

    @Override
    public List<DailyTrainStationQueryResp> queryByTrain(Date trainDate, String trainCode) {
        List<DailyTrainStation> stationList = dailytrainstationRepository.lambdaQuery()
                .eq(DailyTrainStation::getTrainCode, trainCode)
                .eq(DailyTrainStation::getDate, trainDate)
                .eq(DailyTrainStation::getIsDeleted, LogicEnum.NOT_DELETED.getCode())
                .orderByAsc(DailyTrainStation::getIndex).list();
        return CopyUtil.copyList(stationList,DailyTrainStationQueryResp.class);
    }
}
